
#include <iostream>
#include <stdexcept>

namespace Tux { namespace Socket
{
    template <typename DT, typename STT>
    void Socket<DT,STT>::Bind(typename DT::SocketAddress const& address)
    {
        int result = bind(mFD, reinterpret_cast<struct sockaddr const*>(&address),
                          DT::SizeOfSocketAddress(address));
        if (result)
        {
            std::cout << mFD << " " << errno << ": " << strerror(errno) << std::endl;
            throw std::runtime_error("unable to bind"); // TODO: better exception
        }
    }

    template <typename DT, typename STT>
    Socket<DT,STT>::Socket()
        : mFD(socket(DT::AF_VALUE, STT::SOCK_VALUE, 0))
    {
        if (mFD <= 0)
            throw std::runtime_error("unable to create socket"); // TODO: different exception
    }

    template <typename DT, typename STT>
    Socket<DT,STT>::~Socket()
    {
        close(mFD);
    }
}}

